/*
    * Copyright (c) 2024 Hunan OpenValley Digital Industry Development Co., Ltd.
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    *
    *     http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the License for the specific language governing permissions and
    * limitations under the License.
 */

#include "file_share_proxy.h"
#include "jsonUtil.hpp"

namespace OHOS {
namespace FileShare {
int32_t FileShareProxy::SendFile(SendTask &task)
{
    LOGD("SendFile start.");
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    sptr<IRemoteObject> remote = Remote();
    CHKPTR(remote, FS_NULL_PARAM);

    nlohmann::json jsonObject;
    SendTaskToJson(task, jsonObject);
    std::string taskStr = jsonObject.dump();
    data.WriteString(taskStr);

    int32_t ret = remote->SendRequest(SEND_FILE, data, reply, option);
    if (ret != FS_SUCCESS) {
        LOGE("sendRequest failed, ret:%{public}d", ret);
        return FS_PROXY_SEND_REQUEST_FAILED;
    }

    int32_t errorcode = reply.ReadInt32();

    return errorcode;
}

int32_t FileShareProxy::RegisterFileShareCallback(const sptr<IFileShareCallback> &callback)
{
    LOGD("RegisterFileShareCallback start.");
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    sptr<IRemoteObject> remote = Remote();
    CHKPTR(remote, FS_NULL_PARAM);

    if (!data.WriteRemoteObject(callback->AsObject())) {
        LOGE("WriteRemoteObject failed");
        return FS_REGISTER_CALLBACK_FAILED;
    }

    int32_t ret = remote->SendRequest(REGISTER_FILE_SHARE_CALLBACK, data, reply, option);
    if (ret != FS_SUCCESS) {
        LOGE("sendRequest failed, ret:%{public}d", ret);
        return FS_PROXY_SEND_REQUEST_FAILED;
    }

    int32_t errorcode = reply.ReadInt32();

    return errorcode;
}

int32_t FileShareProxy::UnRegisterFileShareCallback()
{
    LOGD("UnRegisterFileShareCallback start.");
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    sptr<IRemoteObject> remote = Remote();
    CHKPTR(remote, FS_NULL_PARAM);

    int32_t ret = remote->SendRequest(UNREGISTER_FILE_SHARE_CALLBACK, data, reply, option);
    if (ret != FS_SUCCESS) {
        LOGE("sendRequest failed, ret:%{public}d", ret);
        return FS_PROXY_SEND_REQUEST_FAILED;
    }

    int32_t errorcode = reply.ReadInt32();

    return errorcode;
}

int32_t FileShareProxy::CancelSendFile(const std::string &deviceId, SendType type)
{
    LOGD("CancelSendFile start.");
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    sptr<IRemoteObject> remote = Remote();
    CHKPTR(remote, FS_NULL_PARAM);

    data.WriteString(deviceId);
    data.WriteInt32((int32_t) type);

    int32_t ret = remote->SendRequest(CANCEL_SEND_FILE, data, reply, option);
    if (ret != FS_SUCCESS) {
        LOGE("sendRequest failed, ret:%{public}d", ret);
        return FS_PROXY_SEND_REQUEST_FAILED;
    }

    int32_t errorcode = reply.ReadInt32();

    return errorcode;
}

int32_t FileShareProxy::SetShareStatus(const bool status)
{
    LOGD("SetShareStatus start.");
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    sptr<IRemoteObject> remote = Remote();
    CHKPTR(remote, FS_NULL_PARAM);

    data.WriteBool(status);

    int32_t ret = remote->SendRequest(SET_SHARE_STATUS, data, reply, option);
    if (ret != FS_SUCCESS) {
        LOGE("sendRequest failed, ret:%{public}d", ret);
        return FS_PROXY_SEND_REQUEST_FAILED;
    }

    int32_t errorcode = reply.ReadInt32();

    return errorcode;
}

int32_t FileShareProxy::GetShareStatus(bool &status)
{
    LOGD("GetShareStatus start.");
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    sptr<IRemoteObject> remote = Remote();
    CHKPTR(remote, FS_NULL_PARAM);

    int32_t ret = remote->SendRequest(GET_SHARE_STATUS, data, reply, option);
    if (ret != FS_SUCCESS) {
        LOGE("sendRequest failed, ret:%{public}d", ret);
        return FS_PROXY_SEND_REQUEST_FAILED;
    }

    status = reply.ReadBool();

    int32_t errorcode = reply.ReadInt32();

    return errorcode;
}

std::string FileShareProxy::GetStoragePath()
{
    LOGD("GetStoragePath start.");
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    sptr<IRemoteObject> remote = Remote();
    if(remote  == nullptr){
        return "";
    }

    int32_t ret = remote->SendRequest(GET_STORAGE_PATH, data, reply, option);
    if (ret != FS_SUCCESS) {
        LOGE("sendRequest failed, ret:%{public}d", ret);
        return "";
    }

    return  reply.ReadString();
}

int32_t FileShareProxy::SetStoragePath(std::string &path)
{
    LOGD("SetStoragePath start.");
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    sptr<IRemoteObject> remote = Remote();
    CHKPTR(remote, FS_NULL_PARAM);
    
    data.WriteString(path);
    int32_t ret = remote->SendRequest(SET_STORAGE_PATH, data, reply, option);
    if (ret != FS_SUCCESS) {
        LOGE("sendRequest failed, ret:%{public}d", ret);
        return FS_PROXY_SEND_REQUEST_FAILED;
    }
    return FS_SUCCESS;
}

int32_t FileShareProxy::GetWorkDuration()
{
    LOGD("GetWorkDuration start.");
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    sptr<IRemoteObject> remote = Remote();
    CHKPTR(remote, FS_NULL_PARAM);

    int32_t ret = remote->SendRequest(GET_WORK_TIME, data, reply, option);
    if (ret != FS_SUCCESS) {
        LOGE("sendRequest failed, ret:%{public}d", ret);
        return FS_PROXY_SEND_REQUEST_FAILED;
    }
    return  reply.ReadInt32();
}

int32_t FileShareProxy::SetWorkDuration(int32_t &time)
{
    LOGD("SetWorkDuration start.");
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    sptr<IRemoteObject> remote = Remote();
    CHKPTR(remote, FS_NULL_PARAM);
    
    data.WriteInt32(time);
    int32_t ret = remote->SendRequest(SET_WORK_TIME, data, reply, option);
    if (ret != FS_SUCCESS) {
        LOGE("sendRequest failed, ret:%{public}d", ret);
        return FS_PROXY_SEND_REQUEST_FAILED;
    }
    return FS_SUCCESS;
}

int32_t FileShareProxy::RegisterShareStatusCallback(const std::string &bundleName, const sptr<IFileShareCallback> &callback)
{
    LOGD("RegisterShareStatusCallback start.");
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    sptr<IRemoteObject> remote = Remote();
    CHKPTR(remote, FS_NULL_PARAM);

    data.WriteString(bundleName);

    if (!data.WriteRemoteObject(callback->AsObject())) {
        LOGE("WriteRemoteObject failed");
        return FS_REGISTER_CALLBACK_FAILED;
    }

    int32_t ret = remote->SendRequest(REGISTER_SHARE_STATUS_CALLBACK, data, reply, option);
    if (ret != FS_SUCCESS) {
        LOGE("sendRequest failed, ret:%{public}d", ret);
        return FS_PROXY_SEND_REQUEST_FAILED;
    }

    int32_t errorcode = reply.ReadInt32();

    return errorcode;
}

int32_t FileShareProxy::UnRegisterShareStatusCallback(const std::string &bundleName)
{
    LOGD("UnRegisterShareStatusCallback start.");
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    sptr<IRemoteObject> remote = Remote();
    CHKPTR(remote, FS_NULL_PARAM);

    data.WriteString(bundleName);

    int32_t ret = remote->SendRequest(UNREGISTER_SHARE_STATUS_CALLBACK, data, reply, option);
    if (ret != FS_SUCCESS) {
        LOGE("sendRequest failed, ret:%{public}d", ret);
        return FS_PROXY_SEND_REQUEST_FAILED;
    }

    int32_t errorcode = reply.ReadInt32();

    return errorcode;
}

int32_t FileShareProxy::ChangeUuid(const uint16_t uuid)
{
    LOGD("ChangeUuid start.");
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    sptr<IRemoteObject> remote = Remote();
    CHKPTR(remote, FS_NULL_PARAM);

    data.WriteInt32(static_cast<int32_t>(uuid));
    int32_t ret = remote->SendRequest(CHANGE_UUID, data, reply, option);
    if (ret != FS_SUCCESS) {
        LOGE("sendRequest failed, ret:%{public}d", ret);
        return FS_PROXY_SEND_REQUEST_FAILED;
    }

    int32_t errorcode = reply.ReadInt32();

    return errorcode;
}
} // namespace FileShare
} // namespace OHOS

